package algocasts;

public class P74_RemoveDuplicatesFromSortedListII {
    class ListNode {
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode deleteDuplicates(ListNode head) {
        ListNode vnode = new ListNode(0);
        vnode.next = head;
        ListNode pre = vnode, cur = pre.next;
        while (cur != null) {
            while (cur.next != null && cur.val == cur.next.val) cur = cur.next;
            if (pre.next != cur) pre.next = cur.next;
            else pre = pre.next;
            cur = pre.next;
        }
        return vnode.next;
    }
}
